.global g_pfnVectors
.global TrapEntry_default

/* start address for the initialization values of the .data section. defined in linker script */
.word _sidata
/* start address for the .data section. defined in linker script */
.word _sdata
/* end address for the .data section. defined in linker script */
.word _edata
/* start address for the .bss section. defined in linker script */
.word _sbss
/* end address for the .bss section. defined in linker script */
.word _ebss


.section .text.Reset_Handler
.weak Reset_Handler
.type Reset_Handler, %function
Reset_Handler:
	/** set global pointer */
.option push
.option norelax
	la gp, __global_pointer$
.option pop
	/** set stack pointer */
	la sp, _estack


	/* Call the clock system initialization function.*/
	call SystemInit

	/* Copy the data segment initializers from flash to SRAM */
	la t0, _sdata
	la t1, _edata
	la t2, _sidata
	bne t0, t2, LoopCopyDataInit
	j CopyDataEnd

CopyDataInit:
	lw a0, 0(t2)
	sw a0, 0(t0)
	addi t2, t2, 4
	addi t0, t0, 4

LoopCopyDataInit:
	bltu t0, t1, CopyDataInit

CopyDataEnd:
	/* Zero fill the bss segment. */
	la t0, _sbss
	la t1, _ebss
	j LoopFillZerobss

FillZerobss:
	sw zero, 0(t0)
	addi t0, t0, 4

LoopFillZerobss:
	bltu t0, t1, FillZerobss

	/* Call static constructors */
	la a0, __libc_fini_array
	call atexit
	call __libc_init_array
	/* Set mtvec: vectored mode*/
	la t0, g_pfnVectors
	ori t0, t0, 0x1
	csrw mtvec, t0

	/* Call the application's entry point.*/
	call  _premain_init
#ifdef SUPPORT_RTOS
	call entry
#else
	call main

	call _postmain_fini
	tail exit
#endif

LoopForever:
	j LoopForever


.size Reset_Handler, .-Reset_Handler

/**
 * @brief  This is the code that gets called when the processor receives an
 *         unexpected interrupt.  This simply enters an infinite loop, preserving
 *         the system state for examination by a debugger.
 *
 * @param  None
 * @retval : None
*/
.section .text.TrapEntry_default,"ax",%progbits
TrapEntry_default:
	j TrapEntry_default

.size TrapEntry_default, .-TrapEntry_default

/******************************************************************************
*
* The minimal vector table for ibex.  Note that the proper constructs
* must be placed on this to ensure that it ends up at physical address
* 0x0000.0000.
*
*****************************************************************************/
.section .isr_vector,"a",%progbits
.type g_pfnVectors, %object
.size g_pfnVectors, .-g_pfnVectors

g_pfnVectors:
	.word TrapEntry_xcpt           /* 0 Exception                   */
	.word TrapEntry_default        /* 1                             */
	.word TrapEntry_default        /* 2                             */
	.word TrapEntry_msip           /* 3 Software interrupt          */
	.word TrapEntry_default        /* 4                             */
	.word TrapEntry_default        /* 5                             */
	.word TrapEntry_default        /* 6                             */
	.word TrapEntry_mtip           /* 7 Timer interrupt             */
	.word TrapEntry_default        /* 8                             */
	.word TrapEntry_default        /* 9                             */
	.word TrapEntry_default        /*10                             */
	.word TrapEntry_meip           /*11 PLIC interrupt              */
	.word TrapEntry_default        /*12                             */
	.word TrapEntry_default        /*13                             */
	.word TrapEntry_default        /*14                             */
	.word TrapEntry_default        /*15                             */
	.word TrapEntry_uartx          /*16 fast, local inetrrupts  0   */
	.word TrapEntry_rtc            /*17 fast, local inetrrupts  1   */
	.word TrapEntry_iwdt           /*18 fast, local inetrrupts  2   */
	.word TrapEntry_wwdt           /*19 fast, local inetrrupts  3   */
	.word TrapEntry_default        /*20 fast, local inetrrupts  4   */
	.word TrapEntry_default        /*21 fast, local inetrrupts  5   */
	.word TrapEntry_default        /*22 fast, local inetrrupts  6   */
	.word TrapEntry_default        /*23 fast, local inetrrupts  7   */
	.word TrapEntry_default        /*24 fast, local inetrrupts  8   */
	.word TrapEntry_default        /*25 fast, local inetrrupts  9   */
	.word TrapEntry_default        /*26 fast, local inetrrupts 10   */
	.word TrapEntry_default        /*27 fast, local inetrrupts 11   */
	.word TrapEntry_default        /*28 fast, local inetrrupts 12   */
	.word TrapEntry_default        /*29 fast, local inetrrupts 13   */
	.word TrapEntry_default        /*30 fast, local inetrrupts 14   */
	.word TrapEntry_nmi            /*31 Non-maskable interrupt(NMI) */


.weak TrapEntry_xcpt
.set  TrapEntry_xcpt, TrapEntry_default

.weak TrapEntry_msip
.set  TrapEntry_msip, TrapEntry_default

.weak TrapEntry_mtip
.set  TrapEntry_mtip, TrapEntry_default

.weak TrapEntry_meip
.set  TrapEntry_meip, TrapEntry_default

.weak TrapEntry_uartx 
.set  TrapEntry_uartx, TrapEntry_default

.weak TrapEntry_rtc    
.set  TrapEntry_rtc, TrapEntry_default

.weak TrapEntry_iwdt
.set  TrapEntry_iwdt, TrapEntry_default

.weak TrapEntry_wwdt
.set  TrapEntry_wwdt, TrapEntry_default

.weak TrapEntry_nmi
.set  TrapEntry_nmi, TrapEntry_default

.weak main
.set main, TrapEntry_default
